#!/usr/bin/env bash
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"

cmd-run() {
  declare desc="runs command in container based on app image"
  declare cmd="run"
  [[ "$1" == "$cmd" ]] && shift 1
  declare APP=""

  declare -a RUN_ENV
  RUN_ENV=()
  while [[ $# -gt 0 ]]; do
    case $1 in
      -e=* | --env=*)
        local arg=$(printf "%s" "$1" | sed -E 's/(^-e=)|(^--env=)//g')
        RUN_ENV+=("$arg")
        shift
        ;;
      -e | --env)
        if [[ ! $2 ]]; then
          dokku_log_warn "expected $1 to have an argument"
          break
        fi
        RUN_ENV+=("$2")
        shift 2
        ;;
      *)
        APP="$1"
        shift
        break
        ;;
    esac
  done

  verify_app_name "$APP"

  local DOKKU_SCHEDULER=$(get_app_scheduler "$APP")
  plugn trigger scheduler-run "$DOKKU_SCHEDULER" "$APP" "${#RUN_ENV[@]}" "${RUN_ENV[@]}" "$@"
}

cmd-run "$@"
